package com.ruoyi.tongban.androidapi.service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ruoyi.tongban.androidapi.domain.AndroidLoginMessage;
import com.ruoyi.tongban.androidapi.domain.AndroidMessageBase;
import com.ruoyi.tongban.androidapi.domain.AndroidSocketSession;
import com.ruoyi.tongban.androidapi.util.AndroidBeanUtils;
import com.ruoyi.tongban.androidapi.util.AndroidSessionCache;
import com.ruoyi.tongban.domain.FaceTvDevice;
import com.ruoyi.tongban.domain.InsChild;
import org.yeauty.pojo.Session;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author caiyanshan
 * @Date 2021/4/21 9:17
 * @Param
 * @Return
 */
public class AndroidApiService {
  
  
  /**
   * @Description 首次连接发送消息,原样返回
   * @Author caiyanshan
   * @Date 2021/4/13 14:09
   * @Param [json, session]
   * @Return java.lang.String
   */
  public String login(String json, Session session){
    Gson gson = new Gson();
    AndroidLoginMessage loginMessage = gson.fromJson(json, AndroidLoginMessage.class);
    AndroidSocketSession webSocketSession = new AndroidSocketSession();
    webSocketSession.setCreateTime(new Date());
    webSocketSession.setSession(session);
    webSocketSession.setSn(loginMessage.getSn());
    AndroidSessionCache.setSession(loginMessage.getSn(), webSocketSession);
    AndroidBeanUtils.getFaceTvDeviceService().insertDevice(loginMessage.getSn());
    return gson.toJson(loginMessage);
  }
  
  /**
   * @Description 心跳,原样返回
   * @Author caiyanshan
   * @Date 2021/4/13 14:11
   * @Param []
   * @Return java.lang.String
   */
  public String ping(String json){
    return json;
  }
  
  /**
   * @Description 获取班级信息
   * @Author caiyanshan
   * @Date 2021/4/21 9:38
   * @Param [json]
   * @Return java.lang.String
   */
  public String getClassInfo(String json){
    Gson gson = new Gson();
    AndroidMessageBase loginMessage = gson.fromJson(json, AndroidMessageBase.class);
    FaceTvDevice faceTvDevice = AndroidBeanUtils.getFaceTvDeviceService().getClassInfoBySn(loginMessage.getSn());
    if(faceTvDevice == null){
      return "";
    }
    loginMessage.setData(faceTvDevice);
    return gson.toJson(loginMessage);
  }
  
  public String listAllChild(String json){
    Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd HH:mm:ss").create();
    AndroidMessageBase loginMessage = gson.fromJson(json, AndroidMessageBase.class);
    String sn = loginMessage.getSn();
    List<InsChild> childList = AndroidBeanUtils.getFaceTvDeviceService().listAllChild(sn);
    loginMessage.setData(childList);
    return gson.toJson(loginMessage);
  }
  
  public String sendChildrenSign(String json){
    return json;
  }
  
  /**
   * @Description 机构按班级分组获取孩子信息
   * @Author caiyanshan
   * @Date 2021/5/21 11:18
   * @Param [json]
   * @Return java.lang.String
   */
  public String listClassInDeptAllChild(String json){
    Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd HH:mm:ss").create();
    AndroidMessageBase loginMessage = gson.fromJson(json, AndroidMessageBase.class);
    String sn = loginMessage.getSn();
    Map<String,Object> childList = AndroidBeanUtils.getFaceTvDeviceService().listDeptChild(sn);
    loginMessage.setData(childList);
    return gson.toJson(loginMessage);
  }
  
}
